<?php

//附件
class UpfController extends EController
{


    //过滤器
    public function filters()
    {
        return array(
            array(
                'application.filters.BackEndAuthRequestFilter',
            )
        );
    }


	public  $layout = 'main';
	private $tag=""  ; //前缀
	private $dir;   //上传路径
	private $auto='true';//自动上传
	private $style="0";
	private $ext = '*.gif;*.jpg;*.png;*.jpeg;*.bmp';//上传文件类型限制
	private $max_num =10;//最大上传文件数目
	private $max_size = '2000KB';//最大上传文件大小
	private $multi='true';//允许上传多个
	private $rename=1;   //重命名 1 重命名
	private $model="public"; //上传图片模块 
	private $allselect=1; //1 全部选择  
	private $thumb=""; //缩略//$thumb="100X129,400X129"; //缩略
	private $thumbImg=""; //缩略//$thumb="100X129,400X129"; //缩略
	private $url ;
	private $shui; //水印图片 例如 1.png
	private $st; //缩图 例如 192*168
	private $pic;

	public function init()
	{
        //$this->pic = "uploads/oday/content/2014/07/17/1.jpg";
		$this->url='/img';
  		//(-------------------------接受传值-------------------)
		//文件格式
		if(isset($_REQUEST['ext'])&&$_REQUEST['ext']!='')
		{
			$this->ext = $_REQUEST['ext'];
		}
		//水印图
		if(isset($_REQUEST['thumbImg'])&&$_REQUEST['thumbImg']!='')
		{
			$this->thumbImg = $_REQUEST['thumbImg'];
		}
		//缩略大小
		if(isset($_REQUEST['thumb'])&&$_REQUEST['thumb']!='')
		{
			$this->thumb = $_REQUEST['thumb'];
		}

		//最大上传文件数目初始化
		if(isset($_GET['max_num'])&&$_GET['max_num']!='')
		{
			$this->max_num = $_GET['max_num'];
		}
		//允许上传多个
		if(isset($_GET['multi'])&&$_GET['multi']!='')
		{
			$this->multi = $_GET['multi'];
		}
		//自动上传
		if(isset($_GET['auto'])&&$_GET['auto']!='')
		{
			$this->auto = $_GET['auto'];
		}
		//最大上传大小
		if(isset($_GET['max_size'])&&$_GET['max_size']!='')
		{
			$this->max_size = $_GET['max_size'];
		}
		//模块名称
		if(isset($_REQUEST['model'])&&$_REQUEST['model']!='')
		{
			$this->model = "aiunify";
		//	echo $this->model;
		}

		if(isset($_REQUEST['rename']))
		{
			$this->rename = $_REQUEST['rename'];
		}
		/*shui*/

		if(isset($_REQUEST['shui']))
		{
			$this->shui = $_REQUEST['shui'];
		}	

		if(isset($_REQUEST['st']))
		{
			$this->st = $_REQUEST['st'];
		}	

		$tname = "file";
		//预处理
		if(!empty($_REQUEST['style']))
		{
			$tname="contents";
			$this->style = 1; 
		}
		$this->dir =  $this->url."/".$this->model."/".$tname."/".date("Y")."/".date("m")."/".date("d")."/";
		if($this->thumb){
			$this->thumb =  explode(",", $this->thumb);
		}
		if($tbimg = $this->thumbImg){
			if(!stristr($tbimg,"无水印")) $tbimg = $tbimg.",无水印|0";
			$this->thumbImg =  explode(",", $tbimg);
		}
	}
	public function actionIndex()
	{
 		$dirs =  str_replace($this->url."/", "", $this->dir);
        $this->render("h5Index",
		  	array(
				"auto"=>$this->auto,
				"ext"=>$this->ext,
				"max_num"=>$this->max_num,
				"max_size"=>$this->max_size,
				"multi"=>$this->multi,  
				"thumb"=>$this->thumb, 
				"thumb1"=>$_GET['thumb'],
				"thumbImg"=>$this->thumbImg, 
				"thumbImg1"=>$_GET['thumbImg'], 
				"model"=>$this->model, 
				// "dirlist"=>$allfiles,  
				"dirs"=>$dirs, 
				"allselect"=>$this->allselect, 
				"style"=>$this->style,
		    )
		);
	}

	//上传动作
    public function actionupload()
    {
    	$targetFolder1 =  str_replace('/img',"",$this->dir); // Relative to the root
    	$targetFolder =   $this->dir ; // Relative to the root
    	$fileTypes= array();
    	$verifyToken = md5('unique_salt' . $_POST['timestamp']);
    	$this->model = $_POST["model"];
    	if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    		$tempFile = $_FILES['Filedata']['tmp_name'];



    		// dirname(dirname(Yii::app()->BasePath))
    		$targetPath = $_SERVER['DOCUMENT_ROOT'] ."/". $targetFolder;
    		// folder(dirname(dirname(Yii::app()->BasePath)).$targetFolder);
    		//创建文件夹
            if(!file_exists(dirname(dirname(Yii::app()->BasePath)).$targetFolder)){
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                @mkdir(dirname(dirname(Yii::app()->BasePath)).$targetFolder,0777,true);
            }

    		//重命名判断  
    		$this->rename = 1;
    		if($this->rename)
    			$targetFile =  '/'.$this->tag.''.date("Ymdgis").rand(0, 1000).".".fileext($_FILES['Filedata']['name']) ;
    		else
    			$targetFile =   '/'. str_replace(" ", "", ($_FILES['Filedata']['name']));
    		$targetFile1 = iconv("UTF-8","gb2312", $targetFile);
    		$ar = explode(";",$this->ext);
    		foreach($ar as $type)
    		{
    			$fileTypes[] = str_replace("*.", "", $type);
    		}

    		$fileParts = pathinfo($_FILES['Filedata']['name']);
    	    if (in_array($fileParts['extension'],$fileTypes)) {

                $image_info = getimagesize($tempFile);


                if (!$image_info) {
                 //   die('Invalid file type.');
                }


    	    	move_uploaded_file($tempFile,rtrim($targetPath,'/') .$targetFile1);
    	    	$output = str_replace("..".$this->url,$this->url, rtrim($targetFolder,'/').$targetFile);
				$file = iconv("UTF-8","gb2312", $output);
				$file= str_replace($this->url."/", "", $file);
				$this->pic = $file;

				$p = dirname(dirname(Yii::app()->BasePath)).$output;

		        //$suo = $this->scthumb($p,600);//缩略图
		       // $output2 = str_replace('/opt/www/fam365_admin','', $suo);

                $output = rtrim($targetPath,'/') .$targetFile1;

		        $res = $output."|"."https://img.bandai-card.com.cn/".str_replace("img/aiunify/","",str_replace($_SERVER['DOCUMENT_ROOT']."/","",$output))."|".$this->model;
    	    	echo $res;
     		} 
     		else
     		{
    			echo 'Invalid file type.';
    		}
    	}
    }

}